﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.Models.PUB;
using Newtonsoft.Json;
using Apps.Models;
using Apps.BLL.HC;
using Apps.BLL.HuaChan;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Text.RegularExpressions;
using Apps.Models.HuaChan;
using Apps.Models.CustomModels;

namespace Apps.Web.Areas.HuaChan.Controllers
{
    public class HuaChanReportController : BaseController
    {
        HUACHAN_BLL huachanBll = new HUACHAN_BLL();
        ValidationErrors errors = new ValidationErrors();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList()
        {
            string startDate = Utils.ObjectToStr(Request["startDate"]);
            string endDate = Utils.ObjectToStr(Request["endDate"]);
            if(string.IsNullOrEmpty(startDate))
                startDate = DateTime.Now.AddMonths(-3).ToString("yyyy-MM-dd");
            if (string.IsNullOrEmpty(endDate))
                endDate = DateTime.Now.ToString("yyyy-MM-dd");
            string isExamine = Utils.ObjectToStr(Request["isExamine"]);
            List<HUACHAN_PROJECT> listProjects = huachanBll.GetAllProject();
            List<HUACHAN_PROJECT_FIELDS> listFields = huachanBll.GetAllFields();
            List<HUACHAN_PROJECT_DATA> data = huachanBll.GetAllData(startDate, endDate, isExamine);
            int maxLen = 0;
            //获取当前数据 中包含的项目
            var currentProject = data.OrderBy(m => m.PROJECTID).GroupBy(m => m.PROJECTID).Select(m => m.Key).ToList();
            List<HUACHAN_PROJECT_DATA> returnData = new List<HUACHAN_PROJECT_DATA>();
            var currentFields = new List<HUACHAN_PROJECT_FIELDS>();
            currentProject.ForEach(res =>
            {
                currentFields = listFields.Where(m => m.PROJECTID == res).ToList();
                //插入标头
                var header = new HUACHAN_PROJECT_DATA
                {
                    ID = "表头",
                    StrAddTime = "",
                    ADDTIME = DateTime.Now,
                    EXAMINE = "",
                    EXMINETIME = "",
                    PROJECTID = listProjects.Where(m => m.ID == res).FirstOrDefault().NAME,
                    DATA1 = GetName(listFields, res, 1),
                    DATA2 = GetName(listFields, res, 2),
                    DATA3 = GetName(listFields, res, 3),
                    DATA4 = GetName(listFields, res, 4),
                    DATA5 = GetName(listFields, res, 5),
                    DATA6 = GetName(listFields, res, 6),
                    DATA7 = GetName(listFields, res, 7),
                    DATA8 = GetName(listFields, res, 8),
                    DATA9 = GetName(listFields, res, 9),
                    DATA10 = GetName(listFields, res, 10),
                    DATA11 = GetName(listFields, res, 11),
                    DATA12 = GetName(listFields, res, 12),
                    DATA13 = GetName(listFields, res, 13),
                    DATA14 = GetName(listFields, res, 14),
                    DATA15 = GetName(listFields, res, 15),
                    DATA16 = GetName(listFields, res, 16),
                    DATA17 = GetName(listFields, res, 17),
                    DATA18 = GetName(listFields, res, 18),
                    DATA19 = GetName(listFields, res, 19),
                    DATA20 = GetName(listFields, res, 20),
                    DATA21 = GetName(listFields, res, 21),
                    DATA22 = GetName(listFields, res, 22),
                    DATA23 = GetName(listFields, res, 23),
                    DATA24 = GetName(listFields, res, 24),
                    DATA25 = GetName(listFields, res, 25),
                    DATA26 = GetName(listFields, res, 26),
                    DATA27 = GetName(listFields, res, 27),
                    DATA28 = GetName(listFields, res, 28),
                    DATA29 = GetName(listFields, res, 29),
                    DATA30 = GetName(listFields, res, 30),
                    DATA31 = GetName(listFields, res, 31),
                    DATA32 = GetName(listFields, res, 32),
                    DATA33 = GetName(listFields, res, 33),
                    DATA34 = GetName(listFields, res, 34),
                    DATA35 = GetName(listFields, res, 35),
                    DATA36 = GetName(listFields, res, 36),
                    DATA37 = GetName(listFields, res, 37),
                    DATA38 = GetName(listFields, res, 38),
                    DATA39 = GetName(listFields, res, 39),
                    DATA40 = GetName(listFields, res, 40),
                    DATA41 = GetName(listFields, res, 41),
                    DATA42 = GetName(listFields, res, 42),
                    DATA43 = GetName(listFields, res, 43),
                    DATA44 = GetName(listFields, res, 44),
                    DATA45 = GetName(listFields, res, 45),
                    DATA46 = GetName(listFields, res, 46),
                    DATA47 = GetName(listFields, res, 47),
                    DATA48 = GetName(listFields, res, 48),
                    DATA49 = GetName(listFields, res, 49),
                    DATA50 = GetName(listFields, res, 50),
                };
                returnData.Add(header);
                maxLen = maxLen < GetLen(header) ? GetLen(header) : maxLen; 
                var datas = data.Where(m => m.PROJECTID == res).OrderByDescending(m => m.ADDTIME).ToList();
                for (int i = 0; i < datas.Count; i++)
                {
                    datas[i].PROJECTID = (i + 1).ToString();
                    returnData.Add(datas[i]);
                }
                returnData.Add(new HUACHAN_PROJECT_DATA { });
                returnData.Add(new HUACHAN_PROJECT_DATA { });
            });
            return Json(new Pagination.PaginationResult<HUACHAN_PROJECT_DATA>(maxLen, returnData == null ? new List<HUACHAN_PROJECT_DATA>() : returnData));
        }

        public int GetLen(HUACHAN_PROJECT_DATA value)
        {
            int len = 0;
            if (value == null)
                len = 0;
            if (!string.IsNullOrEmpty(value.DATA1))
                len = 1;
            if (!string.IsNullOrEmpty(value.DATA2))
                len = 2;
            if (!string.IsNullOrEmpty(value.DATA3))
                len = 3;
            if (!string.IsNullOrEmpty(value.DATA4))
                len = 4;
            if (!string.IsNullOrEmpty(value.DATA5))
                len = 5;
            if (!string.IsNullOrEmpty(value.DATA6))
                len = 6;
            if (!string.IsNullOrEmpty(value.DATA7))
                len = 7;
            if (!string.IsNullOrEmpty(value.DATA8))
                len = 8;
            if (!string.IsNullOrEmpty(value.DATA9))
                len = 9;
            if (!string.IsNullOrEmpty(value.DATA10))
                len = 10;

            if (!string.IsNullOrEmpty(value.DATA11))
                len = 11;
            if (!string.IsNullOrEmpty(value.DATA12))
                len = 12;
            if (!string.IsNullOrEmpty(value.DATA13))
                len = 13;
            if (!string.IsNullOrEmpty(value.DATA14))
                len = 14;
            if (!string.IsNullOrEmpty(value.DATA15))
                len = 15;
            if (!string.IsNullOrEmpty(value.DATA16))
                len = 16;
            if (!string.IsNullOrEmpty(value.DATA17))
                len = 17;
            if (!string.IsNullOrEmpty(value.DATA18))
                len = 18;
            if (!string.IsNullOrEmpty(value.DATA19))
                len = 19;
            if (!string.IsNullOrEmpty(value.DATA20))
                len = 20;

            if (!string.IsNullOrEmpty(value.DATA21))
                len = 21;
            if (!string.IsNullOrEmpty(value.DATA22))
                len = 22;
            if (!string.IsNullOrEmpty(value.DATA23))
                len = 23;
            if (!string.IsNullOrEmpty(value.DATA24))
                len = 24;
            if (!string.IsNullOrEmpty(value.DATA25))
                len = 25;
            if (!string.IsNullOrEmpty(value.DATA26))
                len = 26;
            if (!string.IsNullOrEmpty(value.DATA27))
                len = 27;
            if (!string.IsNullOrEmpty(value.DATA28))
                len = 28;
            if (!string.IsNullOrEmpty(value.DATA29))
                len = 29;
            if (!string.IsNullOrEmpty(value.DATA30))
                len = 30;

            if (!string.IsNullOrEmpty(value.DATA31))
                len = 31;
            if (!string.IsNullOrEmpty(value.DATA32))
                len = 32;
            if (!string.IsNullOrEmpty(value.DATA33))
                len = 33;
            if (!string.IsNullOrEmpty(value.DATA34))
                len = 34;
            if (!string.IsNullOrEmpty(value.DATA35))
                len = 35;
            if (!string.IsNullOrEmpty(value.DATA36))
                len = 36;
            if (!string.IsNullOrEmpty(value.DATA37))
                len = 37;
            if (!string.IsNullOrEmpty(value.DATA38))
                len = 38;
            if (!string.IsNullOrEmpty(value.DATA39))
                len = 39;
            if (!string.IsNullOrEmpty(value.DATA40))
                len = 40;

            if (!string.IsNullOrEmpty(value.DATA41))
                len = 41;
            if (!string.IsNullOrEmpty(value.DATA42))
                len = 42;
            if (!string.IsNullOrEmpty(value.DATA43))
                len = 43;
            if (!string.IsNullOrEmpty(value.DATA44))
                len = 44;
            if (!string.IsNullOrEmpty(value.DATA45))
                len = 45;
            if (!string.IsNullOrEmpty(value.DATA46))
                len = 46;
            if (!string.IsNullOrEmpty(value.DATA47))
                len = 47;
            if (!string.IsNullOrEmpty(value.DATA48))
                len = 48;
            if (!string.IsNullOrEmpty(value.DATA49))
                len = 49;
            if (!string.IsNullOrEmpty(value.DATA50))
                len = 50;
            return len;
        }

        public string GetName(List<HUACHAN_PROJECT_FIELDS> listFields, string projectId, int COLUMNIDX)
        {
            try
            {
                if (listFields == null)
                    return "";
                if (listFields.Count <= 0)
                    return "";
                var data = listFields.Where(m => m.PROJECTID == projectId && m.COLUMNIDX == COLUMNIDX).ToList();
                if (data == null)
                    return "";
                if (data.Count <= 0)
                    return "";
                return data.FirstOrDefault().FIELDNAME;
            }
            catch
            {
                return "";
            }
        }
    }
}